package org.his.controller.quanxian;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.his.dao.RoleDao;
import org.his.pojo.quanxuan.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleDao roleDao;

    @RequestMapping("/index")
    public String index() throws Exception{
        return "quanxian/role/index";
    }

    @RequestMapping("/form")
    public String form() throws Exception {
        return "quanxian/role/form";
    }

    @RequestMapping("/search")
    @ResponseBody
    public List<Role> search() throws Exception {
        return roleDao.getAll();
    }

    @RequestMapping("/view")
    @ResponseBody
    public Role view(Integer roleid) throws Exception{
        return roleDao.get(roleid);
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, Role condition) throws Exception{
        Map<String, Object> map = new HashMap<>();
        Page<Role> pager = PageHelper.startPage(page, limit);



        map.put("data", roleDao.getList(condition));
        map.put("count", pager.getTotal());
        map.put("code", 0);
        map.put("msg", null);
        return map;
    }



    @RequestMapping("/batchDelete")
    @ResponseBody
    public Map<String,Object> batchDelete(Integer ids) throws Exception{
        Map<String,Object> map = new HashMap<>();
        roleDao.delete(ids);
        return map;
    }


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> add(Role role) throws Exception{
        Map<String,Object> map = new HashMap<>();
        roleDao.add(role);
        return map;
    }

    @RequestMapping(value = "edit",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object>edit(Role role) throws Exception{
        Map<String,Object>map=new HashMap<>();

        roleDao.update(role);
        return map;
    }

    @PostMapping("/assignPers")
    @ResponseBody
    public Map<String,Object> assignPers(Integer roleId ,String perIds) throws Exception{
        Map<String,Object> map = new HashMap<>();

        roleDao.deletePersByRoleId(roleId);
        roleDao.addRolePers(roleId,perIds.split(","));

        return map;
    }
}

